package com.whoisaa.wifitransfer.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.whoisaa.wifitransfer.ui.MainActivity;
import com.whoisaa.wifitransfer.common.Consts;
import com.whoisaa.wifitransfer.utils.L;
import com.whoisaa.wifitransfer.utils.NetUtils;
import com.whoisaa.wifitransfer.utils.T;

import java.util.List;

/**
 * @Description Activity基类
 * @Author AA
 * @DateTime 16/3/15 上午11:44
 */
public class BaseActivity extends AppCompatActivity {

    /**
     * 上下文实例
     */
    private static Context mContext;

    /**
     * 资源实例
     */
    private static Resources mResources;

    private long _firstTime = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();
        mResources = getResources();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 获得上下文
     * @return
     */
    protected Context getAppContext() {
        return mContext;
    }

    /**
     * 获得资源对象
     * @return
     */
    protected Resources getAppResources() {
        return mResources;
    }


    public boolean isEmpty(Object obj) {
        boolean result = false;
        if (obj == null) {
            result = true;
        } else if (obj instanceof TextView) {
            //TextView
            TextView tv = (TextView) obj;
            if (TextUtils.isEmpty(tv.getText())) {
                result = true;
            }
        } else if (obj instanceof EditText) {
            //EditText
            EditText et = (EditText) obj;
            if (TextUtils.isEmpty(et.getText())) {
                result = true;
            }
        } else if (obj instanceof String) {
            //String
            String s = (String) obj;
            if (s.trim().equals("")) {
                result = true;
            }
        } else if (obj instanceof List) {
            List<?> list = (List<?>) obj;
            if (list == null || list.size() <= 0) {
                result = true;
            }
        }
        return result;
    }

    public void writeErrorLogcat(String log) {
        L.error(log);
    }

    public boolean isNetWorkAvailable() {
        return NetUtils.isConnected(this);
    }

    public void toast(String msg) {
        T.showShort(this, msg);
    }

    public void toast(int resId) {
        T.showShort(this, resId);
    }

    public void pushActivity(Intent intent) {
        startActivity(intent);
    }

    public void pushActivity(Class<?> mClass) {
        startActivity(new Intent(this, mClass));
    }

    public void pushActivity(Intent intent, boolean finishSelf) {
        pushActivity(intent);

        if (finishSelf)
            finish();
    }

    public void pushActivity(Class<?> mClass, boolean finishSelf) {
        pushActivity(mClass);

        if (finishSelf)
            finish();
    }

    public void pushActivityForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode);
    }

    public void pushActivityForResult(Class<?> mClass, int requestCode) {
        startActivityForResult(new Intent(this, mClass), requestCode);
    }

    /**
     * 优雅地退出App
     * @param context
     */
    protected void exitApp(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(Consts._Exit, true);
        pushActivity(intent);
    }

    /**
     * 再按一次退出程序
     *
     * @param seconds 两次按键时间间隔
     */
    protected void exitAppAfterSeconds(final long seconds) {
        long secondTime = System.currentTimeMillis();
        if (secondTime - _firstTime > seconds) {// 如果两次按键时间间隔大于2000毫秒，则不退出
            toast("再按一次退出程序");
            _firstTime = secondTime;// 更新firstTime
            return;
        } else {
            this.finish();
        }
    }

    /**
     * 发送Handler消息
     * @param handler
     * @param what
     * @param obj
     */
    protected void sendMessage(Handler handler, int what, String obj) {
        Message msg = new Message();
        msg.what = what;
        msg.obj = obj;
        handler.sendMessage(msg);
    }
}
